﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Microhard.Core
{
    public class CryptoHelper
    {
      
        public CryptoHelper()
        {
        }


        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param>
        /// <exception cref="Exception"></exception>
        /// <returns></returns> 
        public static string DesEncrypt(string strText)
        {

            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            string encryptKey = "!@#$%^&^"; //密钥
            byte[] byKey = Encoding.UTF8.GetBytes(encryptKey);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();


            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {

                ms.Close();
                ms.Dispose();
                if (cs != null)
                {
                    cs.Close();
                    cs.Dispose();
                }


            }




        }


        /// <summary>  
        /// 解密字符串  
        /// </summary>  
        /// <param name="inputString">加了密的字符串</param>  
        public static string DesDecrypt(string inputString)
        {

            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byte[] inputByteArray = new Byte[inputString.Length];

            string decryptKey = "!@#$%^&^";
            byte[] byKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();


            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                inputByteArray = Convert.FromBase64String(inputString);
                cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                Encoding encoding = new UTF8Encoding();
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                ms.Close();
                ms.Dispose();
                if (cs != null)
                {
                    cs.Close();
                    cs.Dispose();

                }


            }


        }



    }
}


